<template>
	<view class="wxbind">
		<!-- #ifdef APP-PLUS -->
		<button @click="init" type="primary">绑定微信</button>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<button open-type="getUserInfo" @getuserinfo="init" type="primary">绑定微信</button>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: ''
			};
		},
		onLoad() {
			// #ifdef MP-WEIXIN
			uni.login({
				provider: 'weixin',
				success: (loginRes) => {
					this.code = loginRes.code
				}
			})
			// #endif
		},
		methods: {
			init(e){
				let _this = this
				// #ifdef H5
				uni.showModal({
					title: '温馨提示',
					content: 'H5暂不支持绑定微信，请移步小程序或APP绑定',
					showCancel: false,
					confirmText: '确定',
					success:(res) => {
						if(res.confirm){
							uni.switchTab({
								url: '/pages/index/index'
							})
						}
					}
				})
				// #endif
				// #ifdef APP-PLUS
				uni.login({
					provider: 'weixin',
					success(res) {
						uni.getUserInfo({
							provider: 'weixin',
							success: (userInfo) => {
								let userInfoData = {
									nickname: userInfo.userInfo.nickName,
									headimgurl:  userInfo.userInfo.avatarUrl,
									openID: userInfo.userInfo.openID,
									gender: userInfo.userInfo.gender,
									city: userInfo.userInfo.city,
									province: userInfo.userInfo.province,
									country: userInfo.userInfo.country,
									unionId: userInfo.userInfo.unionId
								}
								let data = {
									code: res.code,
									type: 2,
									phone: JSON.parse(uni.getStorageSync('userInfo')).phone,
									userInfo: JSON.stringify(userInfoData)
								}
								_this.$api.NewAppBinding(data).then(res => {
									uni.showToast({
										icon: 'none',
										title: '绑定成功',
										duration: 1500
									})
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/index/index'
										})
									},1500)
								})
							}
						})
					}
				})
				// #endif
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						uni.checkSession({
							success: () => {
								console.log(e)
								this.code = loginRes.code
								console.log(this.code)
								let data = {
									code: this.code,
									encryptedData: e.detail.encryptedData,
									iv: e.detail.iv,
									phone: JSON.parse(uni.getStorageSync("userInfo")).phone,
									client_type: 2
								}
								console.log(data)
								this.$api.WeChatWxLogin(data).then(res => {
									uni.showToast({
										icon: 'none',
										title: '绑定成功',
										duration: 1500
									})
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/index/index'
										})
									},1500)
								})
							},
							fail: () => {
								uni.login({
									provider: 'weixin',
									success: (loginRes) => {
										this.code = loginRes.code
									}
								})
							}
						})
					}
				})
				// #endif
			}
		}
	}
</script>

<style lang="scss">
	.wxbind{
		padding: 50rpx;
	}
</style>
